package org.mall.modules.shop.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mall.common.api.vo.CommonResult;
import org.mall.modules.shop.entity.ShopClerkRelationShip;
import org.mall.modules.shop.mapper.ShopClerkRelationShipMapper;
import org.mall.modules.shop.mapper.ShopMapper;
import org.mall.modules.shop.service.IShopClerkRelationShipService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 店铺店员关联关系
 * @Author: jeecg-boot
 * @Date:   2019-10-30
 * @Version: V1.0
 */
@Service
public class ShopClerkRelationShipServiceImpl extends ServiceImpl<ShopClerkRelationShipMapper, ShopClerkRelationShip> implements IShopClerkRelationShipService {

    @Resource
    ShopClerkRelationShipMapper shopClerkRelationShipMapper;
    @Resource
    ShopMapper shopMapper;
    /**
     * 批量解除店员
     * @param shopId
     * @param list
     * @return
     */
    @Override
    public int removeByShopIdsAndClerkIds(String shopId, List<String> list) {
        return shopClerkRelationShipMapper.removeByShopIdsAndClerkIds(shopId,list);
    }
    /**
     * 商铺-通过商铺id和店员id删除与店员的关系
     * @param shopId
     * @param clerkId
     */
    @Override
    public void deleteOneShopClerk(String shopId,String clerkId){
        shopClerkRelationShipMapper.deleteShopClerk(shopId,clerkId);
    }
    /**
     * 商铺批量-通过商铺id和店员id删除与店员的关系
     * @param shopIds
     * @param clerkIds
     */
    @Override
    public void deleteListShopClerk(String shopIds,List<String> clerkIds){
        shopClerkRelationShipMapper.deleteShopClerks(shopIds,clerkIds);
    }
    /**
     * 给店铺分配店员，，，，，分配之前，先把店员原先和店铺的关系删除
     * @param clerkId
     * @return
     */
    @Override
    public boolean deleteAllReaOfClerk(String clerkId) {
        return shopClerkRelationShipMapper.deleteAllReaOfClerk(clerkId);
    }

    @Override
    public ShopClerkRelationShip getOneByUserIdAndShopId(String userId, String shopId) {
        return shopClerkRelationShipMapper.getOneByUserIdAndShopId(userId,shopId);
    }

    @Override
    public CommonResult<Object> sellerIsInShop(String sellerId, String shopId) {
       ShopClerkRelationShip db1=shopClerkRelationShipMapper.getOneByUserIdAndShopId(sellerId,shopId);
        if (db1==null){
           ShopClerkRelationShip db2=shopMapper.getShopSellerShipBySellerIdAndShopId(sellerId,shopId);
            if (db2==null){
                return CommonResult.error("");
            }
        }
        return CommonResult.ok();

    }

    @Override
    public List<String> getListByShopId(String s) {
        HashMap map=new HashMap();
        map.put("shop_id",s);
        List<ShopClerkRelationShip> list=  shopClerkRelationShipMapper.selectByMap(map);
        return  list.stream().map(o->{return o.getClerkId()==null?"":o.getClerkId();}).collect(Collectors.toList());

    }


}
